package controllers;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import models.Community;
import models.User;
import play.db.jpa.JPA;
import play.mvc.Controller;

public class Communities extends Controller {
	/**
	 * the method below render the page of community creation
	 * 
	 * @author ismail omar
	 */
	public static void communityCreation() {
		render();
	}

	/**
	 * the below method is for creating a community
	 * 
	 * @param name
	 *            name of the community
	 * @param email
	 *            email of the community
	 * 
	 *            @ author ismail omar
	 */
	public static void createCommunity(String name) {
		String userId = session.get("userId");
		Long userIdOne = Long.parseLong(userId);
		User user = User.findById(userIdOne);
		List<Community> community = user.ownedCommunityList;
		for (int i = 0; i < community.size(); i++) {
			if (community.get(i).name.equalsIgnoreCase(name)) {
				flash.error("This name already existed");
				Communities.communityCreation();
			}
		}
		Community communityOne = new Community(name, user).save();
		user.ownedCommunityList.add(communityOne);
		user.save();
		render(communityOne, user);
	}

	/**
	 * deletes a specific community
	 * 
	 * @param id
	 *            the id of the community to be deleted
	 * @author ismail omar
	 */
	public static void deleteCommunity(Long id) {
		Community community = Community.findById(id);
		
		String userId = session.get("userId");
		Long userIdOne = Long.parseLong(userId);
		User user = User.findById(userIdOne);

		String notificationMessage = " has deleted "
		+ community.name + " Community";
		user.sendNotificationWithType(notificationMessage,
		community.subscribersToCommunity, "Community", community.id);

		List<Community> userCommunityList = user.ownedCommunityList;

		for (int j = 0; j < community.subscribersToCommunity.size(); j++) {

			community.subscribersToCommunity.get(j).userCommunity
					.remove(community);
			community.subscribersToCommunity.get(j).save();
			community.subscribersToCommunity.clear();
			community.save();
			System.out.println("FirstLoop tamaam");
		}
		System.out.println("ahlan beek");
		for (int i = 0; i < userCommunityList.size(); i++) {
			if (userCommunityList.get(i).name.matches(community.name)) {
				user.ownedCommunityList.remove(community);
				community.owner = null;
				user.save();
				community.save();
				community.name = "null";
				community.save();
				System.out.println("Second loop tamaaam");
				Query query = JPA.em().createQuery(
						"DELETE FROM Community WHERE name=null");
				query.executeUpdate();

			}
		}
		render();

	}

	/**
	 * the below method gets all the communities that the user owns and puts
	 * them in a list and then renders it
	 * 
	 * @author ismail omar
	 */
	public static void getUserCommunities() {
		String userId = session.get("userId");
		Long userIdOne = Long.parseLong(userId);
		User user = User.findById(userIdOne);

		List<Community> communityList = user.ownedCommunityList;
		render(user, communityList);
	}

	/**
	 * gets all the subscribers to this community
	 * 
	 * @author ismail omar
	 */
	public static void getAllSubscribers(Long id) {
		Community community = Community.findById(id);
		List<User> subscribers = community.subscribersToCommunity;
		render(subscribers, community);
	}

	/**
	 * gets all the communities that the user is subscribed to it
	 * 
	 * author ismail omar
	 */
	public static void getAllCommunities() {
		String userId = session.get("userId");
		Long userIdOne = Long.parseLong(userId);
		User user = User.findById(userIdOne);

		List<Community> communityList = user.userCommunity;
		render(user, communityList);
	}

	/**
	 * The below method takes an id as a parameter and find the corresponding
	 * community and show it in a new Page upon clicking on it
	 * 
	 * @param id
	 *            the community id
	 * @author ismail omar
	 */
	public static void showCommunity(Long id) {
		Community community = Community.findById(id);
		render(community);
	}

	/**
	 * The below method is for adding a user to a specific community
	 * 
	 * @param id
	 *            community id
	 * @param userEmail
	 *            the user e-mail
	 * 
	 * @author ismail omar
	 */
	public static void addUser(Long id, String userEmail) {
		Community community = Community.findById(id);

		String userId = session.get("userId");
		Long userIdOne = Long.parseLong(userId);
		User userOwner = User.findById(userIdOne);

		List<User> user = User.findAll();
		for (int i = 0; i < user.size(); i++) {
			if (user.get(i).email.matches(userEmail)) {
				community.subscribersToCommunity.add(user.get(i));
				community.save();
				user.get(i).userCommunity.add(community);
				user.get(i).save();
				List<User> addedUser = new ArrayList();
				addedUser.add(user.get(i));
				String notificationMessage = " has added you " + "in "
						+ community.name + " Community";
				userOwner.sendNotificationWithType(notificationMessage,
						addedUser, "Community", community.id);
				render();
			}
		}
		flash.error("Their is no such user");
		Communities.showCommunity(community.id);
	}
	/**
	 * Deletes a user from a community
	 * 
	 * @param id
	 *            id of the community
	 * @param userEmail
	 *            email of the user to be deleted
	 * 
	 * @author ismail omar
	 */
	public static void deleteUser(Long id, String email) {
		Community community = Community.findById(id);
		String userId = session.get("userId");
		Long userIdOne = Long.parseLong(userId);
		User userOwner = User.findById(userIdOne);
		List<User> user = community.subscribersToCommunity;
		for (int i = 0; i < user.size(); i++) {
			if (user.get(i).email.matches(email)) {
				List<User> addedUser = new ArrayList();
				addedUser.add(user.get(i));
				user.get(i).userCommunity.remove(community);
				user.get(i).save();
				community.subscribersToCommunity.remove(user.get(i));
				community.save();
				String notificationMessage = " has removed you " + "from "
						+ community.name + " Community";
				userOwner.sendNotificationWithType(notificationMessage,
						addedUser, "Community", community.id);
				render();
			}
		}

	}

	/**
	 * This method renders a list of top 3 community contributors by taking
	 * community id as input
	 * 
	 * @param id
	 *            community id
	 * @author Nourhan El-Nehlawy
	 */
	public static void viewListOfTopCommunityContributors(Long id) {

		String userIddstring = session.get("userId");
		Long userId = Long.parseLong(userIddstring);
		User communityOwner = User.findById(userId);

		Community community = Community.findById(id);

		List<User> topContributors = community
				.viewListOfTopCommunityContributors();
		render(topContributors);
	}

}
